<%-- 
    Document   : edit_profile
    Created on : Apr 25, 2012, 8:57:13 PM
    Author     : Reinhard
--%>

<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="/connect.jspf"%>
<%@page import="model.PostModel, model.TagModel, model.UserModel, model.AchievementModel" %>
<%@page import="java.io.IOException"%>
<%@page import="java.io.FileNotFoundException"%>
<%@page import="java.io.File"%>

<%!
  // example of use: uploadImg(multireq.getFile("img_upload"), uploadDir + "\\" + content);
  public void uploadImg(File file, String savedFilename) throws FileNotFoundException, IOException {
    BufferedImage buffimg = ImageIO.read(file);
    System.out.println("TEST : " + savedFilename + " " + file + " " + buffimg);
    ImageIO.write(buffimg, "jpg", new File(savedFilename));
  }
%>

<%
    String uploadDir = this.getServletContext().getRealPath("avatar");
    MultipartRequest multireq = new MultipartRequest(request, uploadDir, 5 * 1024 * 1024); // maksimum 5 mb

    String content = null;
    int poster_id = Integer.parseInt(session.getAttribute("userid").toString()); 
    int mid = multireq.getFile("avatar").getName().lastIndexOf(".");
    if (mid > 0) {
      String extension = multireq.getFile("avatar").getName().substring(mid + 1, multireq.getFile("avatar").getName().length());
      content = poster_id + "_" + System.currentTimeMillis() + "." + extension;
    } else {
      content = poster_id + "_" + System.currentTimeMillis() + ".jpg";
    }
    uploadImg(multireq.getFile("avatar"), uploadDir + "\\" + content);
    
    String userid = session.getAttribute("userid").toString();
    String avatar = content;
    String email = session.getAttribute("email").toString();
    String about_me = session.getAttribute("about_me").toString();
    
    session.setAttribute("avatar", content);
    
    UserModel um = new UserModel(con);
    um.updateUserInfo(userid, avatar, email, about_me);
    
    pageContext.forward("profile.jsp?id=" + session.getAttribute("userid").toString());

%>

<%@include file="/footer.jsp"%>
